<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 找最小数（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给一个正整数NUM1&#xff0c;计算出新正整数NUM2&#xff0c;NUM2为NUM1中移除N位数字后的结果&#xff0c;需要使得NUM2的值最小。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>1.输入的第一行为一个<a href="https://so.csdn.net/so/search?q&#61;%E5%AD%97%E7%AC%A6%E4%B8%B2&amp;spm&#61;1001.2101.3001.7020" title="字符串">字符串</a>&#xff0c;字符串由0-9字符组成&#xff0c;记录正整数NUM1&#xff0c;NUM1长度小于32。<br /> 2.输入的第二行为需要移除的数字的个数&#xff0c;小于NUM1长度。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出一个数字字符串&#xff0c;记录最小值NUM2。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td>输入</td><td>2615371<br /> 4</td></tr><tr><td>输出</td><td>131</td></tr><tr><td>说明</td><td>无</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题和<a href="https://blog.csdn.net/qfc_128220/article/details/127407642?spm&#61;1001.2014.3001.5502" title="算法 - 移掉 K 位数字_伏城之外的博客-CSDN博客">算法 - 移掉 K 位数字_伏城之外的博客-CSDN博客</a></p> 
<p>相同&#xff0c;但是本题题目描述和用例不清晰&#xff0c;建议看上面leetcode算法题后再来写本题</p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.LinkedList;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String num1 &#61; sc.nextLine();
    int count &#61; Integer.parseInt(sc.nextLine());

    System.out.println(getResult(num1, count));
  }

  private static String getResult(String num1, int removeCount) {
    if (num1.length() &#61;&#61; removeCount) return &#34;0&#34;;

    int remainCount &#61; num1.length() - removeCount;

    LinkedList&lt;Character&gt; stack &#61; new LinkedList&lt;&gt;();
    for (int i &#61; 0; i &lt; num1.length(); i&#43;&#43;) {
      while (stack.size() &gt; 0 &amp;&amp; removeCount &gt; 0 &amp;&amp; stack.getLast() &gt; num1.charAt(i)) {
        stack.removeLast();
        removeCount--;
      }
      stack.add(num1.charAt(i));
    }

    while (stack.size() &gt; remainCount) {
      stack.removeLast();
    }

    while (stack.getFirst() &#61;&#61; &#39;0&#39; &amp;&amp; stack.size() !&#61; 1) {
      stack.removeFirst();
    }

    StringBuilder sb &#61; new StringBuilder();
    for (Character c : stack) sb.append(c);
    return sb.toString();
  }
}
</code></pre> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    const arr &#61; lines[0].split(&#34;&#34;).map((ele) &#61;&gt; parseInt(ele));
    const removeCount &#61; lines[1];

    console.log(getMinValue(arr, removeCount));
  }
});

function getMinValue(arr, removeCount) {
  if (arr.length &#61;&#61;&#61; removeCount) return &#34;0&#34;;

  let remainCount &#61; arr.length - removeCount;

  let stack &#61; [];
  for (let i &#61; 0; i &lt; arr.length; i&#43;&#43;) {
    while (
      stack.length &gt; 0 &amp;&amp;
      removeCount &gt; 0 &amp;&amp;
      stack[stack.length - 1] &gt; arr[i]
    ) {
      stack.pop();
      removeCount--;
    }
    stack.push(arr[i]);
  }

  while (stack.length &gt; remainCount) {
    stack.pop();
  }

  while (stack[0] &#61;&#61;&#61; 0 &amp;&amp; stack.length !&#61;&#61; 1) {
    stack.shift();
  }

  return stack.join(&#34;&#34;);
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
num1 &#61; input()
removeCount &#61; int(input())


# 算法入口
def getResult(num1, removeCount):
    if len(num1) &#61;&#61; removeCount:
        return &#34;0&#34;

    remainCount &#61; len(num1) - removeCount

    stack &#61; []
    for i in range(len(num1)):
        while len(stack) &gt; 0 and removeCount &gt; 0 and stack[-1] &gt; num1[i]:
            stack.pop()
            removeCount -&#61; 1
        stack.append(num1[i])

    while len(stack) &gt; remainCount:
        stack.pop()

    while stack[0] &#61;&#61; &#39;0&#39; and len(stack) !&#61; 1:
        stack.pop(0)

    return &#34;&#34;.join(stack)


# 算法调用
print(getResult(num1, removeCount))
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>